package com.ohs.dal.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.ohs.dal.dao.BookingDAO;
import com.ohs.dal.model.Booking;

public class BookingDAOImpl extends AbstractDAOImpl<Booking> implements BookingDAO {

	public void saveOrUpdate(Booking entity) {
		getHibernateTemplate().saveOrUpdate(entity);
	}

	public List<Booking> list() {
		return getHibernateTemplate().find("from " + Booking.class.getName());
	}

	public Booking get(long id) {
		return (Booking) getHibernateTemplate().get(Booking.class, id);
	}

	public List<Booking> findByRoom(long roomId) {
		return getHibernateTemplate().find("from Booking b where b.room.roomId = " + roomId);
	}

	public int countBookingWithin(long roomId, Date fromDate, Date toDate) {
		Object[] params = new Object[]{fromDate,toDate, roomId};
		return getHibernateTemplate().find("from Booking b where b.bookingDate BETWEEN ? AND ? AND b.room.roomId = ?", params).size();
	}

	public List<Booking> getBookingWithin(long roomId, Date fromDate,
			Date toDate) {
		Object[] params = new Object[]{fromDate,toDate, roomId};
		return getHibernateTemplate().find("from Booking b where b.bookingDate BETWEEN ? AND ? AND b.room.roomId = ? order by b.bookingDate asc", params);
	}

	public List<Booking> findByDate(Date bookingDate) {
		Object[] params = new Object[]{bookingDate};
		return getHibernateTemplate().find("from Booking b where b.bookingDate = ?", params);
	}
	
}
